document.addEventListener("DOMContentLoaded", function () {
    const editor = document.querySelector(".editor");
    const colorPicker = document.getElementById("colorPicker");
    const fontSizeSelector = document.getElementById("fontSize");
    const fontNameSelector = document.getElementById("fontName");

    function formatText(command, value = null) {
        document.execCommand(command, false, value);
    }

    function insertLink() {
        const url = prompt("Entrez l'URL du lien :", "https://");
        if (url) {
            formatText("createLink", url);
        }
    }

    function insertImage() {
        const imageUrl = prompt("Entrez l'URL de l'image :", "https://");
        if (imageUrl) {
            formatText("insertImage", imageUrl);
        }
    }

    function clearFormatting() {
        formatText("removeFormat");
    }

    colorPicker.addEventListener("input", function () {
        formatText("foreColor", this.value);
    });

    fontSizeSelector.addEventListener("change", function () {
        formatText("fontSize", this.value);
    });

    fontNameSelector.addEventListener("change", function () {
        formatText("fontName", this.value);
    });

    editor.addEventListener("paste", function (event) {
        event.preventDefault();
        const text = event.clipboardData.getData("text/plain");
        document.execCommand("insertText", false, text);
    });
});
